//
//  TMGKnownViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/13.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGKnownViewController.h"
#import "TMGKnowNewsTableViewCell.h"
#import "TMGKnownCompanyDesController.h"
#import "STWebViewController.h"
#import "TMKnowNewsModel.h"
#import "TMGNewsModel.h"
#import "UITableView+STNoresult.h"
#import "TKGKnowCompanWebController.h"
@interface TMGKnownViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, assign) NSInteger                     page;
@end

@implementation TMGKnownViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"了解钱包";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 193)];
    bannerImageView.image = [UIImage imageNamed:@"了解钱包_banner"];
    bannerImageView.userInteractionEnabled = YES;
    [header addSubview:bannerImageView];
    
    __weak typeof(self) weakSelf =  self;
    STButton * sppedButton = [[STButton alloc] initWithFrame:CGRectMake(0, bannerImageView.bottom, UIScreenWidth / 2, 55)
                                                     title:@"快速了解"
                                                titleColor:TM_orangeColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    sppedButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [sppedButton st_showRightLine:30];
    [sppedButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedSpeedButton];
    }];
    [header addSubview:sppedButton];
    
    STButton * desButton = [[STButton alloc] initWithFrame:CGRectMake(sppedButton.right, bannerImageView.bottom, UIScreenWidth / 2, 55)
                                                       title:@"公司介绍"
                                                  titleColor:TM_orangeColor
                                                   titleFont:15
                                                cornerRadius:0
                                             backgroundColor:[UIColor whiteColor]
                                             backgroundImage:nil
                                                       image:nil];
    desButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [desButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedDesButton];
    }];
    [header addSubview:desButton];
    

    header.height = desButton.bottom;
    //头部橘色
    UIView * headers = [[UIView alloc] initWithFrame:CGRectMake(0, -header.height, UIScreenWidth, 2 * header.height)];
    headers.backgroundColor = TM_orangeColor;
    [header insertSubview:headers atIndex:0];
    
    
    self.tableView.tableHeaderView = header;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page =1;
        [self sendNewsRequest];
    }];
    [self.tableView bringSubviewToFront:self.tableView.mj_header];
    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 320, UIScreenWidth, 400) title:@"暂无新闻" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGKnowNewsTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 54;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 54)];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 44)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [sectionHeader addSubview:whiteView];
    
    UIView * yeallowView = [[UIView alloc] initWithFrame:CGRectMake(15,0 +10 , 4, 15 )];
    yeallowView.backgroundColor = TM_orangeColor;
    yeallowView.centerY = 22;
    [whiteView addSubview:yeallowView];
    
    NSString * title = @"实时新闻";
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(yeallowView.right + 15, yeallowView.top, 200, 15)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    titleLable.centerY = 22;
    [whiteView addSubview:titleLable];
    
    return sectionHeader;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGKnowNewsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGKnowNewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGNewsModel *model = self.dataSouce[indexPath.row];
    NSString * url = [NSString stringWithFormat:@"http://jshjkd.com/gold/public/home/web/news?id=%@",model.newsID];
    STWebViewController * webvc = [[STWebViewController alloc] initWithUrl:url];
    webvc.progressBackgroundColor = [UIColor whiteColor];
    webvc.progressTintColor = TM_orangeColor;
    webvc.forceTitle = @"新闻详情";
    [self.navigationController pushViewController:webvc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSlectedSpeedButton{
    //web
    STWebViewController * webvc = [[STWebViewController alloc] initWithUrl:@"http://jshjkd.com/gold/public/home/web/quickly_understand"];
    webvc.progressBackgroundColor = [UIColor whiteColor];
    webvc.progressTintColor = TM_orangeColor;
    webvc.forceTitle = @"快速了解";
    webvc.webView.scrollView.scrollEnabled = NO;
    [self.navigationController pushViewController:webvc animated:YES];
}
- (void)onSlectedDesButton{
    
//    TKGKnowCompanWebController * webvc = [[TKGKnowCompanWebController alloc] initWithUrl:@"http://jshjkd.com/gold/public/home/web/company_profile"];
//    webvc.progressBackgroundColor = [UIColor whiteColor];
//    webvc.progressTintColor = TM_orangeColor;
//    webvc.forceTitle = @"公司介绍";
    
    TMGKnownCompanyDesController * vc = [TMGKnownCompanyDesController new];
    [self.navigationController pushViewController:vc animated:YES];

}
#pragma mark --NetWork Method
- (void)sendNewsRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"1" forKey:@"category"];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/quotes/getPopular"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * datas = responseObject[@"data"];
                                                 if (self.page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                 }
                                                 for (NSDictionary * dataDic in datas) {
                                                     TMGNewsModel * model = [TMGNewsModel mj_objectWithKeyValues:dataDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 if (self.dataSouce.count > 4) {
                                                     self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                                                         self.page ++;
                                                         [self sendNewsRequest];
                                                     }];
                                                 }
                                                 
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end
